package com.haiheng.module_main.fragment.bookcar

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.haiheng.common.constant.RourePage
import com.haiheng.common.fragment.RouterFragment
import com.haiheng.library_base.dialog.LoadingDialog
import com.haiheng.library_base.model.BaseViewModel
import com.haiheng.library_base.toast.SizeUtils
import com.haiheng.library_base.toast.ToastUtils
import com.haiheng.module_main.R
import com.haiheng.module_main.adapter.bookcar.BookCarShelfAdapter
import com.haiheng.module_main.databinding.FragmentBookCarShelfBinding
import com.haiheng.module_main.dialog.BookCarClearHintDialog
import com.haiheng.module_main.dialog.ScanDialog
import com.haiheng.module_main.mode.BookCarShelfViewModel

/**
 *
 * @Des: 扫描上架页面
 * @Author: hwz
 * @Date:2024/8/16 0016
 * @Version: 1.0
 */

class BookCarShelfFragment : RouterFragment(), ScanDialog.OnScanResultCallback {
    private lateinit var binding: FragmentBookCarShelfBinding
    private lateinit var loadingDialog : LoadingDialog

    private val viewModel: BookCarShelfViewModel by viewModels()
    val adapter = BookCarShelfAdapter { selectedBook ->
        // 处理选中的书籍
       updateView()
    }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentBookCarShelfBinding.inflate(inflater, container, false)
        return binding.root
    }

    fun updateView(){
        binding.tvSelectNum.setText("选中${adapter.getSelectedBooks().size}本")
    }

    override fun initView() {
        loadingDialog = LoadingDialog(requireContext(), R.style.AvLoadingDialogStyle)

        binding.includeTile.tvTitle.setText("图书上架")
        binding.includeTile.tvClear.visibility = View.VISIBLE
        binding.includeTile.tvCarName.visibility = View.GONE
        binding.includeTile.btnFinishScan.visibility = View.GONE

        binding?.rvList?.layoutManager = LinearLayoutManager(activity)
        binding.rvList.adapter = adapter

        binding.includeBottom.tvBottomLeft.setText("取消上架")
        binding.includeBottom.tvBottomRight.setText("上架")
        binding.includeBottom.tvBottomLeft.setTextColor(activity.getColor(R.color.color_0071FF))
        var btnLeft = binding.includeBottom.slBottomLeft
        var btnRight = binding.includeBottom.slBottomRight
        btnLeft.setStrokeWidth(SizeUtils.dp2px(1f).toFloat())
        btnLeft.setStrokeColor(activity.getColor(R.color.color_0071FF))
        btnLeft.setLayoutBackground(activity.getColor(R.color.white))
        btnLeft.setLayoutBackgroundTrue(activity.getColor(R.color.white_50))
        btnLeft.setBackgroundColor(activity.getColor(R.color.white))
        btnRight.setLayoutBackground(activity.getColor(R.color.color_0071FF))
        btnRight.setLayoutBackgroundTrue(activity.getColor(R.color.btn_press))
        binding.includeBottom.slBottomLeft.setOnClickListener {

        }
        binding.includeBottom.slBottomRight.setOnClickListener {

        }
        binding.includeTile.sflBack.setOnClickListener {
            activity.navigation(RourePage.BOOK_CAR_LOGIN_RESULT)
        }

        binding.includeTile.tvClear.setOnClickListener {
            BookCarClearHintDialog.newInstance()
                .setOnDialogListener(object : BookCarClearHintDialog.OnDialogClickListener{
                    override fun onClear(dialog: BookCarClearHintDialog) {
                        viewModel._bookInfoList.postValue(emptyList())
                        updateView()
//                        loadingDialog.setMessage("正在删除")
//                        loadingDialog.show()
                        dialog.dismiss()
//                        activity.navigation(RourePage.BOOK_CAR_LOGIN_RESULT)
                    }
                }).show(childFragmentManager)

        }
        binding.includeBottom.slBottomRight.setOnClickListener {
            val selectedBooks = adapter.getSelectedBooks()
            if (selectedBooks.isEmpty()){
                ToastUtils.showShort("请选择要上架的书籍")
                return@setOnClickListener
            }
            loadingDialog.setMessage("上架中")
            loadingDialog.show()
            viewModel.addBookShelf(selectedBooks)
        }

        binding.includeBottom.slBottomLeft.setOnClickListener {
//            loadingDialog.show()
            viewModel.deleteSelectedBooks()
        }

        binding.ctvAllSelect.setOnClickListener {
            viewModel.toggleSelectAll(binding.ctvAllSelect.isChecked)
        }



    }


    override fun initViewModel(): BaseViewModel? {
        viewModel.faiMsg.observe(viewLifecycleOwner){
            loadingDialog.hide()
            if ("上架成功".equals(it)){
                viewModel.deleteSelectedBooks()
            }
            ToastUtils.showShort(it)
            updateView()
        }
        viewModel.bookInfoList.observe(viewLifecycleOwner) {books->
            val groupedBooks = books.groupBy { it.fixedBookShelfLayerName ?: "未知科室" }
            (binding.rvList.adapter as? BookCarShelfAdapter)?.updateData(groupedBooks)
            binding.tvBookListNum.setText("总计：${books.size}本")
            updateView()
        }

        return viewModel
    }

    override fun initLoad() {
        viewModel.getBookList()
    }

    override fun onScanSuccess(result: String?) {

    }

    override fun onScanError(error: String?) {

    }
}